package com.beeway.blackgoose.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beeway.blackgoose.BaseApp;
import com.beeway.blackgoose.R;
import com.beeway.blackgoose.activity.AccountMangerActivity;
import com.beeway.blackgoose.activity.CouponActivity;
import com.beeway.blackgoose.activity.LoginActivity;
import com.beeway.blackgoose.activity.MyStoreActivity;
import com.beeway.blackgoose.bean.UserLoginInfo;
import com.beeway.blackgoose.utils.Utils;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class UserFragment extends Fragment {

    private View userView;
    @ViewInject(R.id.user_coupon_tv)
    private TextView userCouponTv;
    @ViewInject(R.id.user_store_tv)
    private TextView userStoreTv;
    @ViewInject(R.id.user_appraise_tv)
    private TextView userAppraiseTv;
    @ViewInject(R.id.user_update_tv)
    private TextView userUpdateTv;
    @ViewInject(R.id.user_about_tv)
    private TextView userAboutTv;
    @ViewInject(R.id.user_clearcache_tv)
    private TextView userClearcaheTv;
    @ViewInject(R.id.user_state_tv)
    private TextView userStateTv;
    @ViewInject(R.id.user_seller_tv)
    private TextView userSellerTv;
    @ViewInject(R.id.user_head_civ)
    private CircleImageView userHeadCiv;
    private boolean isLogin; //是否是登录状态
    private UserLoginInfo info;

    public UserFragment() {
    }

    public static UserFragment getInstance() {
        UserFragment user = new UserFragment();
        return user;

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            info = (UserLoginInfo) getArguments().getSerializable("info");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        userView = inflater.inflate(R.layout.fragment_user, container, false);
        ViewUtils.inject(this, userView);
        return userView;
    }

    @OnClick({R.id.user_coupon_tv, R.id.user_store_tv, R.id.user_appraise_tv, R.id.user_about_tv, R.id.user_update_tv, R.id.user_seller_tv, R.id.user_head_civ, R.id.user_clearcache_tv})
    public void OnClick(View v) {
        switch (v.getId()) {
            //代金券
            case R.id.user_coupon_tv:
                startActivity(new Intent(getActivity(), CouponActivity.class));
                break;
            //我的收藏
            case R.id.user_store_tv:
                startActivity(new Intent(getActivity(), MyStoreActivity.class));
                break;
            //我的评价
            case R.id.user_appraise_tv:
                startActivity(new Intent());
                break;
            //关于
            case R.id.user_about_tv:
                startActivity(new Intent());
                break;
            //检查更新
            case R.id.user_update_tv:
                startActivity(new Intent());
                break;
            //我是商家
            case R.id.user_seller_tv:
                startActivity(new Intent());
                break;
            //头像登录
            case R.id.user_head_civ:  //这里需要进行判断 来确定用户登录没有 如果登录 点击是进入账号管理 否则是进入登录状态
                if (isLogin) {
                    startActivityForResult(new Intent(BaseApp.getContext(), AccountMangerActivity.class).putExtra("info", info), 101);
                } else {
                    startActivityForResult(new Intent(getActivity(), LoginActivity.class), 100);
                }

                break;
            //清理缓存
            case R.id.user_clearcache_tv:
                startActivity(new Intent());
                break;
        }
        Utils.getAnimIn(getActivity());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        BitmapUtils util = new BitmapUtils(BaseApp.getContext());
        util.configDefaultLoadingImage(R.mipmap.ic_launcher);
        util.configDefaultLoadFailedImage(R.mipmap.ic_launcher);
        if (requestCode == 100 && resultCode == 10) {
            isLogin = true;
            Intent in = data;
            info = (UserLoginInfo) in.getSerializableExtra("info");
            userStateTv.setText(info.getConsumer_account());
            util.display(userHeadCiv, info.getFace());
        } else if (requestCode == 100 && resultCode == 10004) {
            userHeadCiv.setImageBitmap(null);
            userStateTv.setText("未登录");
        }
    }
}
